<?php
/*******************************
 *	Identity aJax actions
*******************************
*/

include('../../includes/classes/core.php');

$action = $_REQUEST['act'];
$error = '';
$data = '';

switch ($action) {
	case 'get_edit_page':
		$identity_id		= $_REQUEST['id'];
		$page				= GetPage(GetIdentity($identity_id));

		$data				= array('page'	=> $page);

		break;
	case 'get_list' :
		$count = 		$_REQUEST['count'];
		$hidden = 		$_REQUEST['hidden'];
	  
		$rResult = mysql_query("
								SELECT 		`production_identity`.`id`,
                 							`production_identity`.rs_name,
											`production`.`name`
							    FROM   		`production_identity`
								LEFT JOIN 	`production`
								ON 			`production_identity`.production_id = `production`.`id`
	    		               ");

		$data = array(
				"aaData"	=> array()
		);

		while ( $aRow = mysql_fetch_array( $rResult ) )
		{
			$row = array();
			for ( $i = 0 ; $i < $count ; $i++ )
			{
				/* General output */
				$row[] = $aRow[$i];
				if($i == ($count - 1)){
					$row[] = '<input type="checkbox" name="check_' . $aRow[$hidden] . '" class="check" value="' . $aRow[$hidden] . '" />';
				}
			}
			$data['aaData'][] = $row;
		}

		break;
	case 'save_identity':
		$identity_id	= $_REQUEST['id'];

		$rs_id			= $_REQUEST['rs_id'];
		$local_name		= $_REQUEST['local_name'];
		
		$prod_id = GetProductID($local_name);
		
		SaveIdentity($identity_id, $prod_id);

		break;
	default:
		$error = 'Action is Null';
}

$data['error'] = $error;

echo json_encode($data);


/* ******************************
 *	Identity Functions
* ******************************
*/

function SaveIdentity($identity_id, $prod_id)
{	
	mysql_query("UPDATE `production_identity`
	             SET    `production_identity`.`production_id` = $prod_id
	             WHERE  `id` = $identity_id");
}
	
function GetProductID($prod_name)
{
	$prod_name = htmlspecialchars($prod_name, ENT_QUOTES);
	$res = mysql_fetch_assoc(mysql_query("SELECT `id`
			FROM `production`
			WHERE `name` = '$prod_name'"));
	return $res['id'];
}

function GetIdentity($identity_id)
{
	$res = mysql_fetch_assoc(mysql_query("  SELECT 		`production_identity`.`id` 		AS `id`,
                                      					`production_identity`.`rs_name` AS `rs`,
									   					`production`.`name`		   		AS `local`
											FROM  		`production_identity` LEFT JOIN `production`
												ON 		`production_identity`.`production_id` = `production`.`id`
											WHERE  		`production_identity`.`id` = $identity_id"));
		
	return $res;
}

function GetPage($res = '')
{
	$data = '
		<div id="dialog-form">
			<fieldset>
				<legend>ძირითადი ინფორმაცია</legend>
	
		    	<table class="dialog-form-table">
					<tr>
						<td style="width: 170px;"><label for="category">დასახელება(ზედნადები)</label></td>
						<td>
							<input type="text" id="rs" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" value="' . $res['rs'] . '" disabled="disabled" />
						</td>
				 	</tr>
					<tr>
						<td style="width: 170px;"><label for="category">დასახელება(ლოკალური)</label></td>
						<td>
							<div class="seoy-row" id="prod_name_seoy">
								<input type="text" id="local" class="idle seoy-address" onblur="this.className=\'idle seoy-address\'" onfocus="this.className=\'activeField seoy-address\'" value="' . $res['local'] . '" />
								<button id="local_btn" class="combobox">local</button>
							</div>
						</td>
					</tr>
				</table>
				<!-- ID -->
				<input type="hidden" id="identity_id" value="' . $res['id'] . '" />
	        </fieldset>
    	</div>
    ';
	return $data;
}

?>